//
//  CycleScrollViewController.m
//  GSG
//
//  Created by dllo on 15/5/2.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "CycleScrollViewController.h"
#import "SongListCell.h"
#import "AFNetworking.h"
#import "AFNetworkingTool.h"
#import "SongModel.h"
#import "PlayerViewController.h"
#import"PlayListViewController.h"
#import "MBProgressHUD.h"
#import"singleSong.h"
@interface CycleScrollViewController ()<UIWebViewDelegate>
@property(nonatomic,retain)UITableView *tableview;
@property(nonatomic,retain)NSMutableArray *arr;
@property(nonatomic,copy)NSString *string;
@property(nonatomic,retain)NSMutableArray *arrModel;
@property(nonatomic,retain)UIWebView *webView;
@property(nonatomic,copy)NSString *weburl;
@property(nonatomic,retain)MBProgressHUD *HUD;
@end

@implementation CycleScrollViewController
- (void)dealloc
{
    [_tableview release];
    [_arr release];
    [_arrModel release];
    [_webView release];
    [_HUD release];
    [super dealloc];
}

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
    self=[super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        self.arr=[NSMutableArray array];
        self.arrModel=[NSMutableArray array];
        
    }

    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"播放" style:UIBarButtonItemStyleDone target:self action:@selector(actionBuLand)];
    self.navigationItem.rightBarButtonItem.tintColor=[UIColor whiteColor];
 
    if ([self.url isEqualToString:@""]) {
       [self createTabelVeiw];
      }

 
   else{
       [self createWebView];
   
   }
    

    }
-(void)actionBuLand{
    PlayListViewController *plvc=[[PlayListViewController alloc]init];
    [self.navigationController pushViewController:plvc animated:YES];
    [plvc release];
    
}
-(void)createTabelVeiw{
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bujing"]];

    self.tableview =[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-self.view.frame.size.height*60/667) style:UITableViewStylePlain];
    [self.view addSubview:self.tableview];
    
    [_tableview release];
    self.tableview.backgroundColor = [UIColor clearColor];
    self.tableview.delegate=self;
    self.tableview.dataSource=self;
    self.tableview.rowHeight=100;
    [self data];
    //小菊花
    //方式1.直接在View上show
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    
    
    //常用的设置
    //小矩形的背景色
    _HUD.color = [UIColor clearColor];//这儿表示无背景
    //显示的文字
    _HUD.labelText = @"提示";
    //细节文字
    _HUD.detailsLabelText = @"正在加载内容";
    //是否有庶罩
    _HUD.dimBackground = YES;
    

}
-(void)createWebView{
    //小菊花
    //方式1.直接在View上show
    _HUD = [[MBProgressHUD showHUDAddedTo:self.view animated:YES] retain];
    
    
    //常用的设置
    //小矩形的背景色
    _HUD.color = [UIColor clearColor];//这儿表示无背景
    //显示的文字
    _HUD.labelText = @"提示";
    //细节文字
    _HUD.detailsLabelText = @"正在加载内容";
    //是否有庶罩
    _HUD.dimBackground = YES;
    
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
   
    NSURLRequest *request =[NSURLRequest requestWithURL:[NSURL URLWithString:self.url]];
    [self.view addSubview: _webView];
   
    [_webView loadRequest:request];
  [_HUD hide:YES];
}
-(void)data{
    NSString *str=@"http://v1.ard.q.itlily.com/share/user_timeline?msg_ids=%5B"  ;
    NSString *str2=@"%5D";
    NSString *str3=[NSString stringWithFormat:@"%@%@%@",str,self.ID,str2];
//    NSLog(@"-----%@",str3);
    
  [AFNetworkingTool NetWorkingTooStr:str3 block:^(id result) {
      NSMutableArray *arr=[result objectForKey:@"data"];
      NSMutableDictionary *dic=[NSMutableDictionary dictionary];
      dic=arr[0];
      NSMutableArray *arr2=[NSMutableArray array];
      arr2=[dic objectForKey:@"songlist"];
      for (int i=0;i<arr2.count;i++){
          NSString *str=[NSString stringWithFormat:@",%@",[arr2[i] objectForKey:@"song_id"]];
          [self.arr addObject:str];
      }
      self.string = nil;
      NSString *lastString = @"";
      for (NSString *value in self.arr) {
          self.string = [NSString stringWithFormat:@"%@%@", lastString, value];
          
          lastString = [NSString stringWithFormat:@"%@", self.string];
         
               }
        [self data2];
      [self.tableview reloadData ];
  }];
    


}

-(void)data2{
    
    NSString *str=[NSString stringWithFormat: @"http://ting.hotchanson.com/songs/downwhite?song_id=%@",self.string ];
//    NSLog(@"%@",str);

    [AFNetworkingTool NetWorkingTooStr:str block:^(id result) {
        
        NSMutableArray *arr = [result objectForKey:@"data"];
//        NSLog(@"%@",arr);
        for (NSInteger i = 0; i < arr.count ; i++) {
            SongModel *mo = [[SongModel alloc]init];
            NSMutableDictionary *dic1 = arr[i];
            mo.song_name=[dic1 objectForKey:@"song_name"];
            mo.singer_name=[dic1 objectForKey:@"singer_name"];
            NSMutableArray *arr = [dic1 objectForKey:@"url_list"];
            
            NSMutableDictionary *dic2=arr[0];
            mo.url=[dic2 objectForKey:@"url"];
            
            
            [self.arrModel addObject:mo];
            [mo release];
            
            
            
        }
        
    
        [self.tableview reloadData];
        [_HUD hide:YES];
    
    }];
    
    
}



-(NSInteger )tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return self.arrModel.count;

}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *reuse=@"reuse";
    
    SongListCell *cell=[tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell=[[[SongListCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuse] autorelease];
        
    }
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    SongModel *song=self.arrModel[indexPath.row];
    NSString *str = [NSString stringWithFormat:@"%ld.%@",indexPath.row +1,song.song_name];
    
    cell.songName.text=str;
    cell.singerName.text=song.singer_name;
    return  cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   
//    PlayerViewController *pvc=[[PlayerViewController alloc]init];
//    pvc.arr=self.arrModel;
//    pvc.num=indexPath.row;
//    
//    [self.navigationController pushViewController:pvc animated:YES];
//    [pvc release];
    [singleSong shareSingleSong].arrSong=self.arrModel;
    [singleSong shareSingleSong].num=indexPath.row;
    
    NSNotificationCenter *center=[NSNotificationCenter  defaultCenter];
    
    [center postNotificationName:@"播放" object:@"收到了看见了" userInfo:nil];


}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
